package com.spark.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.constants.validGroup.AddGroup;
import com.spark.common.constants.validGroup.EditGroup;
import com.spark.common.core.Result;
import com.spark.common.core.api.SparkCommonApi;
import com.spark.common.query.QueryGenerator;
import com.spark.common.core.entity.system.SysParamsType;
import com.spark.modules.system.service.ISysParamsTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 系统参数配置类型Controller
 *
 * @author LYCHEE
 * @date 2025/03/22 11:08
 */
@RestController
@RequestMapping(value = "/system/sysParamsType")
public class SysParamsTypeController {

    @Resource
    private ISysParamsTypeService sysParamsTypeService;
    @Resource
    private SparkCommonApi sparkCommonApi;

    /**
     * 初始化系统参数缓存
     */
    @PostConstruct
    public void init(){
        sparkCommonApi.refreshParamsCache(true);
    }

    /**
     * 分页列表查询
     *
     * @param sysParamsType 系统参数配置类型对象
     * @param pageNo   分页
     * @param pageSize 分页
     * @return 系统参数配置类型列表
     */
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(SysParamsType sysParamsType,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<SysParamsType> queryWrapper = QueryGenerator.initQueryWrapper(sysParamsType);
        Page<SysParamsType> page = new Page<SysParamsType>(pageNo, pageSize);
        IPage<SysParamsType> pageList = sysParamsTypeService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param sysParamsType 系统参数配置类型对象
     * @return 系统参数配置类型列表
     */
    @GetMapping(value = "/queryList")
    public Result<?> queryList(SysParamsType sysParamsType) {
        QueryWrapper<SysParamsType> queryWrapper = QueryGenerator.initQueryWrapper(sysParamsType);
        List<SysParamsType> list = sysParamsTypeService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 系统参数配置类型对象
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        SysParamsType sysParamsType = sysParamsTypeService.getById(id);
        if (Objects.isNull(sysParamsType)) {
            return Result.fail("数据获取失败");
        }
        return Result.ok(sysParamsType);
    }

    /**
     * 新增
     *
     * @param sysParamsType 系统参数配置类型对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:params:addTab')")
    @PostMapping(value = "/add")
    public Result<?> add(@Validated(AddGroup.class) @RequestBody SysParamsType sysParamsType) {
        boolean save = sysParamsTypeService.save(sysParamsType);
        return Result.flag(save);
    }

    /**
     * 编辑
     *
     * @param sysParamsType 系统参数配置类型对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:params:editTab')")
    @PostMapping(value = "/edit")
    public Result<?> edit(@Validated(EditGroup.class) @RequestBody SysParamsType sysParamsType) {
        boolean update = sysParamsTypeService.updateById(sysParamsType);
        return Result.flag(update);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('sys:params:delTab')")
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) {
        boolean update = sysParamsTypeService.removeById(id);
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = sysParamsTypeService.removeBatchByIds(Arrays.asList(ids));
        return Result.flag(update);
    }

    /**
     * 刷新缓存
     * @return 成功与否
     */
    @GetMapping(value = "/refreshCache")
    public Result<?> refreshCache(){
        sparkCommonApi.refreshParamsCache(true);
        return Result.ok("刷新成功");
    }

}
